package edu.hwadee.action;

import java.util.List;
import java.util.Map;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ModelDriven;

import edu.hwadee.entity.Area;
import edu.hwadee.entity.Company;
import edu.hwadee.entity.Staff;

/**
* @author 作者  luhaojian:
* @version 创建时间：2018年8月21日 上午9:38:33
* 类说明:
*/
public class StaffTestAction extends BasicAction implements ModelDriven<Staff> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private Staff staff=new Staff();
	private Company company;
	private String address;
	private Integer companyId;
	private Integer paperNumber;
	
	
	

	
	public String login() {
		
		System.out.println("进入login");
		System.out.println(staff.getStaffName()+" "+staff.getPassword());
		String flag="error";
		List<Staff> list=(List<Staff>)getStaffServiceImpl().search(staff);
		System.out.println(list.get(0).getStaffId());
		if(!list.isEmpty())
		{			
				 Staff	staffSession=getStaffServiceImpl().searchById(list.get(0).getStaffId());
				 Area area=getAreaServiceImpl().searchById(staffSession.getArea().getAreaId());
				 Company company=getCompanyServiceImpl().searchById(staffSession.getCompanyByCompany().getCompanyId());
				if(staffSession.getPower()==0)	
					flag = "customer";
				else flag="inside";
					System.out.println("登录成功");
					ActionContext.getContext().getSession().put("staff", staffSession);
					ActionContext.getContext().getSession().put("area", area);
					ActionContext.getContext().getSession().put("company", company);
					
				}
		else {
			ActionContext.getContext().getSession().put("error", true);
			System.out.println("list为空，登录失败");
		}
		System.out.println(flag);
		return flag;

		
		
		
	}
	
	
	
	
	
	public String companyRegister() {
		
		System.out.println("进入注册Action");
		System.out.println(address);
		String flag="error";
		if(address!=null)
		{
			company.setArea(getAreaServiceImpl().searchById(Integer.parseInt(address)));
			System.out.println(company.getArea());
		}
		
		//判断该公司是否已经注册
		Company isExit=new Company();
		isExit.setCompanyName(company.getCompanyName());
		List<Company> companyList=getCompanyServiceImpl().search(isExit);
		//假如该公司已存在
		if(!companyList.isEmpty())
		{
			ActionContext.getContext().getSession().put("companyExist", true);
			ActionContext.getContext().getSession().put("registerSuccess", false);
			ActionContext.getContext().getSession().put("staffExist", false);
			return "companyExist";
		}
		
		//该公司可注册
		Integer i = getCompanyServiceImpl().save(company);
		System.out.println(i);
		if(staff!=null) {
			
			Staff staffExist=new Staff();
			staffExist.setStaffName(staff.getStaffName());
			List<Staff> staffList=getStaffServiceImpl().search(staffExist);
			
			//假如账号已注册
			if(!staffList.isEmpty())
			{
				System.out.println("该账号已存在");
				ActionContext.getContext().getSession().put("staffExist", true);
				ActionContext.getContext().getSession().put("registerSuccess", false);
				ActionContext.getContext().getSession().put("companyExist", false);
				return "staffExist";
				}
			
			//账号未注册
			staff.setCompanyByCompany(getCompanyServiceImpl().searchById(i));
			staff.setPhoneNumber(company.getPhoneNumber());
			staff.setArea(getAreaServiceImpl().searchById(Integer.parseInt(address)));
			staff.setPower((short) 6);
			staff.setProperty("物流商代表");
			if(getStaffServiceImpl().save(staff)) {
				System.out.println("注册成功");
				flag="success";
				ActionContext.getContext().getSession().put("registerSuccess", true);
				ActionContext.getContext().getSession().put("staffExist", false);
				ActionContext.getContext().getSession().put("companyExist", false);
			}
			else {
				System.out.println("注册失败");
			}
		}
		return flag;
		
	}
	
	
	public String searchStaffById() {
		
		Map<String, Object> session=ActionContext.getContext().getSession();
		staff=(Staff)session.get("staff");
		staff=getStaffServiceImpl().searchById(staff.getStaffId());
		if(staff!=null)
		{
			System.out.println("staff不为空");
			return "success";
		}
		else
		{
			return "error";
		}
	}
	
	public String updateStaff() {
		if(staff!=null) {
			
			if(address!=null)
			{
				staff.setArea(getAreaServiceImpl().searchById(Integer.parseInt(address)));
			}
			if(companyId!=null)
			{
				staff.setCompanyByCompany(getCompanyServiceImpl().searchById(companyId));
			}
		
		}
		if(getStaffServiceImpl().update(staff))
			{
				ActionContext.getContext().getSession().put("staff", staff);
				return "success";
				
			}
		else {
			return "error";
		}
	}
	
	@Override
	public Staff getModel() {
		if(staff==null)
			staff=new Staff();
		return staff;
	}


	public Staff getStaff() {
		return staff;
	}


	public void setStaff(Staff staff) {
		this.staff = staff;
	}




	public Integer getPaperNumber() {
		return paperNumber;
	}


	public void setPaperNumber(Integer paperNumber) {
		this.paperNumber = paperNumber;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public Company getCompany() {
		return company;
	}

	public void setCompany(Company company) {
		this.company = company;
	}

	public Integer getCompanyId() {
		return companyId;
	}

	public void setCompanyId(Integer companyId) {
		this.companyId = companyId;
	}

	





	
}
